package Java.com.wang.Tre;

public class BinaryTree<T> {
    private Node<T> root;
    private final  int maxNodes=100;

    public BinaryTree() {
        this.root = new Node<T>();
    }

    public BinaryTree(T x) {
        this.root = new Node<T>(x);
    }

    public boolean insertLeft(T x, Node<T> parent) {
        if (parent == null) {
            return false;
        }
        Node<T> p = new Node<T>(x);
        if (parent.lChild == null) {
            parent.lChild = p;
            System.out.println(parent.lChild);
        } else {
            p.lChild = parent.lChild;
            parent.lChild = p;
            System.out.println(parent.lChild);
        }
        return true;
    }

    public boolean deleteLeft(Node<T> parent) {
        if (parent == null) {
            return false;
        } else {
            parent.lChild = null;
            return true;
        }
    }

    public boolean insertRight(T x, Node<T> parent) {
        if (parent == null) {
            return false;
        }
        Node<T> p = new Node<T>(x);
        if (parent.rChild == null) {
            parent.rChild = p;
            System.out.println(parent.rChild + " ");

        } else {
            p.rChild = parent.rChild;
            parent.rChild = p;
            System.out.println(parent.rChild);
        }
        return true;
    }

    public boolean deleteRight(Node<T> parent) {
        if (parent == null) {
            return false;
        } else {
            parent.rChild = null;
            return true;
        }
    }

    public Node<T> getroot() {
        return this.root;
    }

    public void getall(Node<T> x) {
        System.out.print(x.getData() + "  ");
        System.out.println(x);
    }

    public void levelOrder() {

        Node<T>[] queue = new Node[maxNodes];
        int front,rear;
        if(this.root==null){
            return;
        }
        front=-1;
        rear=0;
        queue[rear]=this.root;
        while (front!=rear){
            front++;
            if(queue[front].lChild!=null){
                rear++;
                queue[rear]=queue[front].lChild;
            }
        }
    }

}
